package com.test.travelinformation.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.qmuiteam.qmui.widget.dialog.QMUIDialogAction;
import com.test.travelinformation.R;
import com.test.travelinformation.adapter.CircleAdapter;
import com.test.travelinformation.dao.TravelDao;
import com.test.travelinformation.entity.Collect;
import com.test.travelinformation.entity.Travel;
import com.test.travelinformation.utils.SPUtils;

import java.net.URI;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.DeleteBatchListener;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.SaveListener;
import cn.bmob.v3.listener.UpdateListener;

public class CollectedActivity extends AppCompatActivity {
    RecyclerView recyclerView;
    CircleAdapter adapte;
    List<Travel> describe = new ArrayList<>();
    List<String> image = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_collected);
        recyclerView = findViewById(R.id.recyclerView);
//        loadData();
        initview();
    }

    private void initview() {
        adapte = new CircleAdapter(this, new ArrayList<>());
        TravelDao travelDao=new TravelDao();
        travelDao.getCollectData(adapte,SPUtils.get(this,"userId","").toString());
        adapte.setOnItemClickListener((adapter, view, position) -> {

        });
        adapte.setOnItemLongClickListener((adapter, view, position) -> {
            new QMUIDialog.MessageDialogBuilder(CollectedActivity.this)
                    .setTitle("取消收藏")
                    .setMessage("确认要取消收藏这条信息吗？")
                    .addAction("取消", (dialog, index) -> dialog.dismiss())
                    .addAction(0, "确定", QMUIDialogAction.ACTION_PROP_NEGATIVE, (dialog, index) -> {
                        Travel travel = ((Travel)adapter.getData().get(position));
                        BmobQuery<Collect> query = new BmobQuery<>();
                        query.addWhereEqualTo("travelId",travel.getObjectId());
                        query.addWhereEqualTo("userId",SPUtils.get(CollectedActivity.this,"userId","").toString());
                        query.addWhereEqualTo("userName",SPUtils.get(CollectedActivity.this,"username","").toString());
                        query.findObjects(new FindListener<Collect>() {
                            @Override
                            public void done(List<Collect> list, BmobException e) {
                                if (e == null){
                                    for (Collect c: list) {
                                        c.delete(new UpdateListener() {
                                            @Override
                                            public void done(BmobException e) {
                                                if (e == null){
                                                    Toast.makeText(CollectedActivity.this,"取消成功",Toast.LENGTH_SHORT).show();
                                                    adapte.setNewInstance(new ArrayList<>());
                                                    TravelDao travelDao=new TravelDao();
                                                    travelDao.getCollectData(adapte,SPUtils.get(CollectedActivity.this,"userId","").toString());
                                                }else {
                                                    Toast.makeText(CollectedActivity.this,"取消失败"+e.toString(),Toast.LENGTH_SHORT).show();
                                                }
                                            }
                                        });
                                    }
                                }
                            }
                        });
                        dialog.dismiss();
                    }).create().show();
            return true;
        });
        recyclerView.setLayoutManager(new GridLayoutManager(this, 1));
        recyclerView.setAdapter(adapte);
    }

    private void loadData() {
        BmobQuery<Travel> bmobQuery = new BmobQuery<Travel>();
        bmobQuery.findObjects(new FindListener<Travel>() {
            @Override
            public void done(List<Travel> list, BmobException e) {
                if (e == null) {
                    Toast.makeText(CollectedActivity.this, "查询成功：共" + list.size() + "条数据。", Toast.LENGTH_LONG).show();
                    for (Travel gameScore : list) {
                        describe.add(gameScore);
                    }
                    adapte.setNewInstance(describe);
                } else {
                    Log.i("bmob", "获取失败：" + e.getMessage() + "," + e.getErrorCode());
                }
            }
        });
    }


}

